﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Gmantis.Controls.Toolbar
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    public class ToolbarPanel : Panel
    {
        protected override Size ArrangeOverride(Size finalSize)
        {
            int count = base.Children.Count;
            double x = 0.0;
            double y = 0.0;
            for (int i = 0; i < count; i++)
            {
                UIElement element = base.Children[i];
                element.Arrange(new Rect(x, y, element.DesiredSize.Width, base.DesiredSize.Height));
                x += element.DesiredSize.Width;
            }
            return finalSize;
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            int count = base.Children.Count;
            double num2 = 0.0;
            for (int i = 0; i < count; i++)
            {
                FrameworkElement element = base.Children[i] as FrameworkElement;
                if (element != null)
                {
                    num2 += element.MinWidth;
                }
            }
            Size size = new Size();
            for (int j = 0; j < count; j++)
            {
                FrameworkElement element2 = base.Children[j] as FrameworkElement;
                if (element2 != null)
                {
                    num2 -= element2.MinWidth;
                }
                UIElement element3 = base.Children[j];
                double width = Math.Max(element2.MinWidth, (availableSize.Width - num2) - size.Width);
                Size size2 = new Size(width, double.PositiveInfinity);
                element3.Measure(size2);
                size.Width += element3.DesiredSize.Width;
                size.Height = Math.Max(size.Height, element3.DesiredSize.Height);
            }
            return size;
        }
    }
}
